package com.lishe.lishe.lishe.module.excitation.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.module.excitation.bean.DivisionObject;
import com.lishe.lishe.lishe.module.excitation.bean.user.UserObject;

import java.util.List;

/**
 * Created by shino on 2017/10/31.
 * 点赞对象适配器
 */

public class DianZanPersonAdapter extends RecyclerviewBaseAdapter {
    private List<DivisionObject> divisions;
    private List<UserObject> users;
    private static final int TYPE_DIVISION = -1;
    private static final int TYPE_PERSON = 1;
    private Activity activity;

    public DianZanPersonAdapter(Activity activity, List<DivisionObject> divisions, List<UserObject> users) {
        this.divisions = divisions;
        this.users = users;
        this.activity = activity;
    }

    int index = -1;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolderMy(ViewGroup parent, int viewType) {
        if (viewType == TYPE_DIVISION) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dianzan_division, null);
            DianZanDivisionHolder holder = new DianZanDivisionHolder(view);
            return holder;
        }
        if (viewType == TYPE_PERSON) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dianzan_person, null);
            DianZanPersonHolder holder = new DianZanPersonHolder(view);
            return holder;
        }
        return null;
    }

    @Override
    public void onBindViewHolderMy(RecyclerView.ViewHolder holder0, int position) {
        if (getItemViewTypeMy(position) == TYPE_DIVISION) {
            index = position;
            DianZanDivisionHolder holder = (DianZanDivisionHolder) holder0;
            DivisionObject divisionObject = divisions.get(position);
            holder.division.setText(divisionObject.getDeptName());
        }
        if (getItemViewTypeMy(position) == TYPE_PERSON) {
            DianZanPersonHolder holder = (DianZanPersonHolder) holder0;
            UserObject user;
            if (index != -1 && index == (position - 1)) {
                ((DianZanPersonHolder) holder0).line.setVisibility(View.VISIBLE);
            } else {
                ((DianZanPersonHolder) holder0).line.setVisibility(View.GONE);
            }
            if (divisions != null) {
                user = users.get(position - divisions.size());
            } else {
                user = users.get(position);
            }
            ImageLoaderUtils.displayImage(activity, user.getAvatarPath(), holder.headImg, R.mipmap.head);
            holder.name.setText(user.getEmpName());
        }

    }

    @Override
    public int getItemCountMy() {
        int count = (divisions == null ? 0 : divisions.size());
        count = count + (users == null ? 0 : users.size());
        return count;
    }

    @Override
    public int getItemViewTypeMy(int position) {
        if (divisions != null) {
            if (position < divisions.size()) {
                return TYPE_DIVISION;
            } else if (position < divisions.size() + users.size()) {
                return TYPE_PERSON;
            }
        }
        return TYPE_PERSON;
    }

    class DianZanDivisionHolder extends RecyclerView.ViewHolder {

        TextView division;

        public DianZanDivisionHolder(View itemView) {
            super(itemView);
            division = itemView.findViewById(R.id.division);
        }
    }

    class DianZanPersonHolder extends RecyclerView.ViewHolder {

        TextView name;
        ImageView headImg;
        View line;

        public DianZanPersonHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            headImg = itemView.findViewById(R.id.headImg);
            line = itemView.findViewById(R.id.line);
        }
    }
}
